﻿CREATE PROCEDURE [dbo].[PartyGoodsUpdate]
(--обновляет партию и первую проводку по приходу 210
	@PartyId int,
	@GoodsId int,
	@Qntty int,
	@RetailPrice decimal(19, 2),
	@BarCode char(12),
	@ReturnLineId int,
	@MD datetime,
	@MW varchar(50),
	@SetNoCount bit
)
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int, @intRowCount int
DECLARE @orgBarCode char(12), @orgGoodsDocLinesId int
DECLARE	@orgDocId int, @orgDocDate smalldatetime, @orgGoodsId int
DECLARE @orgRetailPrice decimal(19, 2), @orgQntty int
DECLARE @intInputCount int, @intOutputCount int

	SET NOCOUNT ON

IF (@PartyId Is Null) OR (@Qntty Is Null) OR (@RetailPrice Is Null) OR (@BarCode Is Null)
BEGIN
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
/*
IF (@PartyId Is Null) SELECT @strMsg = '@PartyId Is Null  ' + @strMsg
IF (@Qntty Is Null) SELECT @strMsg = '@Qntty Is Null  ' + @strMsg
IF (@RetailPrice Is Null) SELECT @strMsg = '@RetailPrice Is Null  ' + @strMsg
IF (@BarCode Is Null) SELECT @strMsg = '@BarCode Is Null  ' + @strMsg
*/
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

SELECT @orgBarCode = P.BarCode, @orgDocId = P.DocID, @orgGoodsDocLinesId = L.Id,
@orgRetailPrice = P.RetailPrice, @orgQntty = L.Qntty, @orgGoodsId = P.GoodsId
FROM CD.Parties P INNER JOIN CA.GoodsDocLines L
ON L.PartyId = P.Id AND L.DocId = P.DocId 
WHERE P.Id = @PartyId
IF (@@rowcount <> 1)
BEGIN
	IF @@trancount > 0 ROLLBACK TRAN
	SELECT @intErrCode = 210, 
	@strMsg = 'Партия с идентификатором ' + cast(@PartyId as varchar(20)) + ' не существует!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF (@Qntty <= 0) OR (@RetailPrice <= 0)
BEGIN
	IF @@trancount > 0 ROLLBACK TRAN
	SELECT @intErrCode = 211, 
	@strMsg = 'Количество и розничная цена д.б. больше нуля!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF ( @orgRetailPrice <> @RetailPrice) --изменилась цена
BEGIN --проверить было ли товародвижение по партии
	IF EXISTS (SELECT Id FROM CA.GoodsDocLines WHERE PartyId = @PartyId AND DocId <> @orgDocId)
	BEGIN
		IF @@trancount > 0 ROLLBACK TRAN
		SELECT @intErrCode = 212, 
		@strMsg = 'По партии ' + @orgBarCode + '(' + cast(@PartyId as varchar(20)) + ') было товародвижение! Цену изменить нельзя'
		RAISERROR (@strMsg, 11, 1)
		RETURN @intErrCode
	END
END

IF @orgGoodsId <> @GoodsId
BEGIN --изменился код товара
--проверить было ли товародвижение по партии
	IF EXISTS (SELECT Id FROM CA.GoodsDocLines WHERE PartyId = @PartyId AND DocId <> @orgDocId)
	BEGIN
		IF @@trancount > 0 ROLLBACK TRAN
		SELECT @intErrCode = 213, 
		@strMsg = 'По партии ' + @orgBarCode + '(' + cast(@PartyId as varchar(20)) + ') было товародвижение! Товар менять нельзя'
		RAISERROR (@strMsg, 11, 1)
		RETURN @intErrCode
	END
END

IF (@orgQntty <> @Qntty) --изменилось количество по приходу
BEGIN 
	SELECT @orgDocDate = DocDate FROM CA.Documents WHERE Id = @orgDocId
	SET @intRowCount = @Qntty - @orgQntty
--проверка возможности изменения количества
	EXEC @intRet = dbo.CheckPartyFlow @PartyId, @intRowCount, @orgDocDate, @intInputCount OUTPUT, @intOutputCount OUTPUT
	IF @intRet <> 0 
	BEGIN
		IF @@trancount > 0 ROLLBACK TRAN
		RETURN @intRet
	END
END

UPDATE [CD].[Parties] SET 
GoodsId = @GoodsId,
[BarCode] = @BarCode, 
[RetailPrice] = @RetailPrice, 
[MD] = @MD, [MW] = @MW 
WHERE Id = @PartyId
SELECT @intRowCount = @@rowcount, @intErrCode = @@error
IF (@intErrCode <> 0)
BEGIN
	IF @@trancount > 0 ROLLBACK TRAN
	RETURN @intErrCode
END
IF (@intRowCount <> 1)
BEGIN
	IF @@trancount > 0 ROLLBACK TRAN
	SELECT @intErrCode = 213, 
	@strMsg = 'Партия ' + @orgBarCode + '(' + cast(@PartyId as varchar(20)) + ') не обновлена!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

IF @SetNoCount = 0 SET NOCOUNT OFF

UPDATE [CA].[GoodsDocLines] SET 
[Qntty] = @Qntty, 
[RetailSum] = @RetailPrice * @Qntty, 
ReturnLineId = @ReturnLineId,
[MD] = @MD, [MW] = @MW 
WHERE Id = @orgGoodsDocLinesId;
SELECT @intRowCount = @@rowcount, @intErrCode = @@error
IF (@intErrCode <> 0)
BEGIN
	IF @@trancount > 0 ROLLBACK TRAN
	RETURN @intErrCode
END
IF (@intRowCount <> 1)
BEGIN
	IF @@trancount > 0 ROLLBACK TRAN
	SELECT @intErrCode = 214, 
	@strMsg = 'Строка товародвижения с идентификатором ' + cast(@orgGoodsDocLinesId as varchar(20)) + ' не обновлена!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

RETURN 0
